<?php

/**
*  @author tungck
*  @filesource home.php
*  
*/


class Home extends CI_Controller{
    
    function __construct(){
        
        parent::__construct();
        $this->load->helper('url');
        $this->load->library(array('form_validation','session'));
        $this->load->library('adminlayout');
        $this->adminlayout->setLayout("admin/layout");
        $userSession = $this->session->userdata('user');
        
        if($userSession['username'] == false){
            redirect(base_url('/index/login?returnUrl=').urlencode(RETURN_URL));
            
        }else{
            if($userSession['useradmin'] != 1){
                redirect(base_url('nhanvien/event/list_event'));
            }
        }
    }
    
    
    function listProfileStaff(){
        
        $title = "Danh sách hồ sơ nhân viên";
        $this->adminlayout->view("admin/list_staff",null ,$title);
        
    }
    
    function getList(){
        $data['sort'] = $_GET["jtSorting"];
        $data['startIndex'] = $_GET["jtStartIndex"];
        $data['pageSize'] = $_GET["jtPageSize"];
        $this->load->model("admin/staff_managers_model");
        $result = $this->staff_managers_model->getList($data);
        $jTableResult = array();
		$jTableResult['Result'] = "OK";
        $jTableResult['TotalRecordCount'] = $result['recordCount'];
		$jTableResult['Records'] = $result['query'];
		print json_encode($jTableResult);
    }
    
    function page(){
        return true;
    }
    
    function bophan(){
        //$id = $_GET['id'];
        $this->load->model("admin/bophan_model");
        $result = $this->bophan_model->getAll();
        $jTableResult = array();
        $rows = array();
        foreach($result as $row)
        {
            $rows[]=array("Value"=>$row->MaBoPhan,"DisplayText"=>$row->TenBoPhan);

        }

		$jTableResult['Result'] = "OK";
        $jTableResult['Options'] = $rows;
        print json_encode($jTableResult);
    }
    
    
    function loaiHopDong(){
        //$id = $_GET['id'];
        $this->load->model("admin/contract_model");
        $result = $this->contract_model->getAll();
        $jTableResult = array();
        $rows = array();
        foreach($result as $row)
        {
            $rows[]=array("Value"=>$row->MaLoaiHopDong,"DisplayText"=>$row->TenLoaiHopDong);

        }

		$jTableResult['Result'] = "OK";
        $jTableResult['Options'] = $rows;
        print json_encode($jTableResult);
    }
    
    function chucDanh(){
        //$id = $_GET['id'];
        $this->load->model("admin/position_model");
        $result = $this->position_model->getAll();
        $jTableResult = array();
        $rows = array();
        foreach($result as $row)
        {
            $rows[]=array("Value"=>$row->MaChucDanh,"DisplayText"=>$row->TenChucDanh);

        }

		$jTableResult['Result'] = "OK";
        $jTableResult['Options'] = $rows;
        print json_encode($jTableResult);
    }
    
    
    function congViec(){
        //$id = $_GET['id'];
        $this->load->model("admin/work_model");
        $result = $this->work_model->getAll();
        $jTableResult = array();
        $rows = array();
        foreach($result as $row)
        {
            $rows[]=array("Value"=>$row->MaCongViec,"DisplayText"=>$row->TenCongViec);

        }

		$jTableResult['Result'] = "OK";
        $jTableResult['Options'] = $rows;
        print json_encode($jTableResult);
    }
    
    
    function getDetail(){
        
        $staffId = $this->input->get('staffId');
        if($staffId !== false){
            $this->load->model("admin/staff_managers_model");
            $this->load->model("admin/work_model");
            $this->load->model("admin/position_model");
            $this->load->model("admin/contract_model");
            $this->load->model("admin/bophan_model");
            $result = $this->staff_managers_model->getDetailStaff($staffId);
            $maCV = $this->work_model->getById($result[0]->MaCongViec);
            $maCD = $this->position_model->getById($result[0]->MaChucDanh);
            $maHD = $this->contract_model->getById($result[0]->MaLoaiHopDong);
            $maBP = $this->bophan_model->getById($result[0]->MaBoPhan);
            $result[0]->TenCongViec = $maCV[0]->TenCongViec;
            $result[0]->TenChucDanh = $maCD[0]->TenChucDanh;
            $result[0]->TenLoaiHopDong = $maHD[0]->TenLoaiHopDong;
            $result[0]->TenBoPhan = $maBP[0]->TenBoPhan;
            $data['data'] = $result[0];
            $title = $result[0]->HoTen;
            $this->adminlayout->view("admin/staffDetail",$data ,$title);
            
        }
        
    }
    
    function addStaff(){
        $jTableResult = array();
        if(isset($_POST['HoTen']) && !empty($_POST['HoTen'])){
            $data['HoTen'] = $_POST['HoTen'];
            $data['SoDienThoai'] = $_POST['SoDienThoai'];
            $data['EmailCaNhan'] = $_POST['EmailCaNhan'];
            $data['EmailCongTy'] = $_POST['EmailCongTy'];
            $data['DiaChiLienHe'] = $_POST['DiaChiLienHe'];
            $data['NgayVaoCongTy'] = $_POST['NgayVaoCongTy'];
            $data['MaBoPhan'] = $_POST['MaBoPhan'];
            $data['MaLoaiHopDong'] = $_POST['MaLoaiHopDong'];
            $data['MaChucDanh'] = $_POST['MaChucDanh'];
            $data['MaCongViec'] = $_POST['MaCongViec'];
            $this->load->model("admin/staff_managers_model");
            $result = $this->staff_managers_model->insertStaff($data);
            $jTableResult['Result'] = "OK";
            $jTableResult['Record'] = $result;
            print json_encode($jTableResult);
        }else{
            $jTableResult['Result'] = "ERROR";
            $jTableResult['Message'] = array('Nhập họ tên');
        	print json_encode($jTableResult);
        }
        
    }
    
    function deleteStaff(){
        try{
        if(isset($_POST["MaHoSo"])){
            $id = $_POST["MaHoSo"];
            $this->load->model("admin/staff_managers_model");
            $this->staff_managers_model->deleteStaff($id);
            $jTableResult = array();
            $jTableResult['Result'] = "OK";
            print json_encode($jTableResult);
            
        }
        }catch(Exception $ex){
            $jTableResult = array();
	        $jTableResult['Result'] = "ERROR";
	           $jTableResult['Message'] = $ex->message();
    	       print json_encode($jTableResult);
        }
        
    }
    
    function updateStaff(){
        
        $data['HoTen'] = $_POST['HoTen'];
        $data['SoDienThoai'] = $_POST['SoDienThoai'];
        $data['EmailCaNhan'] = $_POST['EmailCaNhan'];
        $data['EmailCongTy'] = $_POST['EmailCongTy'];
        $data['DiaChiLienHe'] = $_POST['DiaChiLienHe'];
        $data['NgayVaoCongTy'] = $_POST['NgayVaoCongTy'];
        $data['MaBoPhan'] = $_POST['MaBoPhan'];
        $data['MaLoaiHopDong'] = $_POST['MaLoaiHopDong'];
        $data['MaChucDanh'] = $_POST['MaChucDanh'];
        $data['MaCongViec'] = $_POST['MaCongViec'];
        $data['MaHoSo'] = $_POST['MaHoSo'];
        $this->load->model("admin/staff_managers_model");
        $this->staff_managers_model->updateStaff($data);
        $jTableResult = array();
        if(!empty($data['HoTen'])){
            $jTableResult['Result'] = "OK";
            print json_encode($jTableResult);
        }else{
            $jTableResult['Result'] = "ERROR";
            $jTableResult['Message'] = array('Nhập họ tên');
        	print json_encode($jTableResult);
        }
    }
}

?>